package se.webbzon.boltzmann.object.light;

import java.awt.Color;
import java.nio.ByteBuffer;

import javax.vecmath.Vector3f;

import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.appearance.DirectionalLightAppearance3D;

public class DirectionalLightDeflation extends WorldObjectDeflation {
	
	// The direction of the light
	private float x,y,z;
	
	// The color of the light
	private Color color;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public DirectionalLightDeflation(int deflationProperties) {
		super(deflationProperties,null);
	}
	
	@Friendly DirectionalLightDeflation(int deflationProperties,
										DirectionalLight directionalLight) {
		super(deflationProperties,null);
		DirectionalLightAppearance3D appearance3d = 
				(DirectionalLightAppearance3D) directionalLight.getAppearance();
		Vector3f direction = new Vector3f();
		appearance3d.getDirection(direction);
		x = direction.getX();
		y = direction.getY();
		z = direction.getZ();
		color = appearance3d.getColor();
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void inflate(ByteBuffer buff) {
		x = buff.getFloat();
		y = buff.getFloat();
		z = buff.getFloat();
		int red, green, blue, alpha;
		red = 0x000000FF & buff.get();
		green = 0x000000FF & buff.get();
		blue = 0x000000FF & buff.get();
		alpha = 0x000000FF & buff.get();
		color = new Color(red,green,blue,alpha);
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = ByteBuffer.allocate(allocate + 16);
		buff.putFloat(x);
		buff.putFloat(y);
		buff.putFloat(z);
		buff.put((byte) color.getRed());
		buff.put((byte) color.getGreen());
		buff.put((byte) color.getBlue());
		buff.put((byte) color.getAlpha());
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	/** Creates a new directional light from this serialization. **/
	@Friendly DirectionalLight makeDirectionalLight() {
		return new DirectionalLight(new Vector3f(x,y,z),color);
	}

}
